/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialcontactor.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.potentialcontactor.entity.PotentialContactor;
import cn.knet.sp.ncrm.modules.customer.potentialcontactor.service.PotentialContactorService;

/**
 * 潜在客户主表Controller
 * @author gehaiyang
 * @version 2015-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/potentialContactor")
public class PotentialContactorController extends BaseController {

	@Autowired
	private PotentialContactorService potentialContactorService;
	
	@ModelAttribute
	public PotentialContactor get(@RequestParam(required=false) String id) {
		PotentialContactor entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = potentialContactorService.get(id);
		}
		if (entity == null){
			entity = new PotentialContactor();
		}
		return entity;
	}
	
	@RequiresPermissions("customer:potentialContactor:view")
	@RequestMapping(value = {"list", ""})
	public String list(PotentialContactor potentialContactor, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialContactor> page = potentialContactorService.findPage(new Page<PotentialContactor>(request, response), potentialContactor); 
		model.addAttribute("page", page);
		return "modules/customer/potentialcontactor/potentialContactorList";
	}

	@RequestMapping("save")
	@ResponseBody
	public AjaxMessage save(PotentialContactor potentialContactor) {
		try {
			beanValidator(potentialContactor);
			AjaxMessage ajaxMessage = AjaxMessage.success("保存联系人成功");
			potentialContactorService.save(potentialContactor);
			return ajaxMessage;
		} catch (ConstraintViolationException e) {
			logger.error("验证联系人出现错误", e);
			return AjaxMessage.error("验证联系人出现错误");
		} catch (Exception e) {
			logger.error("保存联系人信息出现错误", e);
			return AjaxMessage.error("保存联系人失败");
		}
	}
	
	//@RequiresPermissions("customer:potentialContactor:view")
	@RequestMapping(value = "listContactorData")
	@ResponseBody
	public Page<PotentialContactor> listContactorData(PotentialContactor potentialContactor) {
		Page<PotentialContactor> page = new Page();
		if (StringUtils.isNotBlank(potentialContactor.getPotentialCustomerId()))
			page.setList(potentialContactorService.findList(potentialContactor));
		return page;
	}
	
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxMessage delete(PotentialContactor potentialContactor) {
		try {
			AjaxMessage ajaxMessage = AjaxMessage.success("删除联系人成功");
			potentialContactorService.delete(potentialContactor);
			return ajaxMessage;
		} catch (Exception e) {
			logger.error("删除联系人信息出现错误", e);
			return AjaxMessage.error("删除联系人失败");
		}
	}

}